<?php
error_reporting(0);
class emailgroup extends CI_Controller 
{
	var $base_url = NULL;
	function __construct() 
	{
		parent::__construct();
		
		$this->load->library('ion_auth');
		$this->load->library('form_validation');
		$this->load->library('session');
		$this->load->library('pagination');
		$this->load->database();
		$this->lang->load('auth');
		$this->lang->load('admin_estate');
		$this->load->helper('language');
		$this->load->model('admin_estate_model');
		
		$this->load->helper('email');
		$this->load->helper('url');	
		$this->load->model('emailgroup_model');
		$this->base_url = base_url();
	}
	
	function index()
	{
    	$data = array();
		$data['suc_msg'] = $this->session->flashdata('suc_msg');
		$data['err_msg'] = $this->session->flashdata('err_msg');
						
		$data['records'] = $this->emailgroup_model->getall_emailgroups();
		
		$data['ptitle'] = 'Group';
		$this->tmadmin->tmView('emailgroup/list_view', $data);
	}
	
	
	function active()
	{
		$ids_post_array = explode(",",$this->input->post('items'));
		foreach($ids_post_array as $index => $poll_id)
			if (is_numeric($poll_id) && $poll_id > 0) 
				$this->poll_model->active_poll($poll_id);
		
		$error = "Selected polls (poll_id's: ".$this->input->post('items').") Active with 
success ";
		$this->output->set_header($this->config->item('ajax_header'));
		$this->output->set_output($error);
		$this->load->view('admin/dashbord',$data);
	}
		
	function inactive()
	{
		$ids_post_array = explode(",",$this->input->post('items'));
		foreach($ids_post_array as $index => $poll_id)
			if (is_numeric($poll_id) && $poll_id > 0) 
				$this->poll_model->inactive_poll($poll_id);
									
		$error = "Selected polls (poll_id's: ".$this->input->post('items').") Inactive with success ";
		$this->output->set_header($this->config->item('ajax_header'));
		$this->output->set_output($error);
		$this->load->view('admin/dashbord',$data);
	}
    function delete()
	{
		$id = $this->input->get('id');
		$this->emailgroup_model->emailgroup_delete($id);
									
		$data['message'] = "Selected Group (id's: ".$this->input->post('items').") delete with success ";
		$this->session->set_flashdata('message', $data['message']);
		
		redirect('emailgroup');
	}
	
    function add($group_id=null)
	{
	    $data = array();
		$post = $_POST;
		//print "<pre>"; print_r($_POST); exit;
		
		$group_id = $this->input->get('group_id');
		
		if(isset($post['save']) and $post['save'] == 'save') { 
			$this->form_validation->set_message('required','Require Field:'.' '.'%s'); 
			$form_valid = array(
								  array(
										 'field'   => 'group_name',
										 'label'   => 'Group Name',
										 'rules'   => 'trim|required'
									  )
								);
			$this->form_validation->set_rules($form_valid);	
			$this->form_validation->set_error_delimiters('<span class="error_message">','</span>'); 		
			if ($this->form_validation->run() == TRUE){
				unset($post['save']);
				//stat the add or update code //
				if(isset($post['edit_group_id']) && $post['edit_group_id'] != ''){
					$group_id = $post['edit_group_id'];
					unset($post['edit_group_id']);
					
					$prefix = '';
					$ids = '';
					foreach($post['group_emaillist'] as $list) {
						$ids .= $prefix ." ".$list;
						$prefix = ',';
					}
						
					$post['group_emaillist'] = $ids;
					
					
					$result=$this->emailgroup_model->emailgroup_update_record($post,$group_id);
					$this->session->set_flashdata('message','Successfully Updated Record.');
					redirect('emailgroup');
				}
				else{
					$prefix = '';
					$ids = '';
					unset($post['edit_group_id']);
					
					foreach($post['group_emaillist'] as $list) {
						$ids .= $prefix ." ".$list;
						$prefix = ',';
					}
					$post['group_emaillist'] = $ids;
					//$group_id = (isset($post['group_id']) && $post['group_id'] > 0)?$post['group_id']:$group_id;
					
					$result=$this->emailgroup_model->emailgroup_insert($post);
					
					$this->session->set_flashdata('message','Successfully Added Record.');                   	
					redirect('emailgroup');
				}
				
				//End the add or update code //
			
			}
			$this->load->view('admin/dashbord',$data);
		}
		if(isset($group_id)) {								
			$data['edit_result'] = $this->emailgroup_model->emailgroup_record($group_id);
			$data['group_id'] = $group_id;
			
			$email_list = $data['edit_result']['group_emaillist'];
			$data['arrGroupUser'] = explode(", ",$email_list);
			
		}
		$data['getall_users'] = $this->emailgroup_model->getall_users();
		
		$data['ptitle'] = 'Add Group';
		$this->tmadmin->tmView('emailgroup/add_view', $data);
    }
}
?>